package com.systop.asset.service.impl;

import com.systop.asset.entity.Asset;
import com.systop.asset.entity.InventoryCheckDetail;
import com.systop.asset.entity.InventoryRecords;
import com.systop.asset.entity.Material;
import com.systop.asset.entity.vo.MaterialListVo;
import com.systop.asset.exception.SystemException;
import com.systop.asset.mapper.AssetMapper;
import com.systop.asset.mapper.InventoryCheckDetailMapper;
import com.systop.asset.mapper.InventoryRecordsMapper;
import com.systop.asset.mapper.MaterialMapper;
import com.systop.asset.service.IInventoryRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 资产盘点记录Service业务层处理
 * 
 * @author lsp
 * @date 2025-07-07
 */
@Service
public class InventoryRecordsServiceImpl implements IInventoryRecordsService 
{

    @Autowired
    private InventoryRecordsMapper inventoryRecordsMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private InventoryCheckDetailMapper inventoryCheckDetailMapper;

    /**
     * 查询资产盘点记录
     * 
     * @param id 资产盘点记录主键
     * @return 资产盘点记录
     */
    @Override
    public InventoryRecords selectInventoryRecordsById(Long id)
    {
        return inventoryRecordsMapper.selectInventoryRecordsById(id);
    }

    /**
     * 查询资产盘点记录列表
     * 
     * @param inventoryRecords 资产盘点记录
     * @return 资产盘点记录
     */
    @Override
    public List<InventoryRecords> selectInventoryRecordsList(InventoryRecords inventoryRecords)
    {
        return inventoryRecordsMapper.selectInventoryRecordsList(inventoryRecords);
    }

    /**
     * 新增资产盘点记录
     * 
     * @param inventoryRecords 资产盘点记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertInventoryRecords(InventoryRecords inventoryRecords)
    {
        inventoryRecords.setOperator((Long) SecurityContextHolder.getContext().getAuthentication().getCredentials());
        inventoryRecordsMapper.insertInventoryRecords(inventoryRecords);
        if (inventoryRecords.getType() == 0) {

            return insertInventoryCheckDetails(inventoryRecords);
        } else if (inventoryRecords.getType() == 1) {
            return insertInventoryCheckMaterialDetails(inventoryRecords);
        } else {
            throw new SystemException(401, "未知盘点类型");
        }
    }

    public int insertInventoryCheckDetails(InventoryRecords inventoryRecords) {
        List<InventoryCheckDetail> inventoryCheckDetails = new ArrayList<>();
        for (Long departmentId : inventoryRecords.getDepartmentIds()) {
            Asset asset = new Asset();
            asset.setDeptId(departmentId);
            List<Asset> assetItems = assetMapper.selectAssetList(asset);
            if (assetItems.isEmpty()) {
                throw new SystemException(500, "部门" + departmentId + "没有资产");
            }
            for (Asset assetItem : assetItems) {
                InventoryCheckDetail detail = new InventoryCheckDetail();
                detail.setAssetId(assetItem.getId());
                detail.setLocationId(assetItem.getLocationId());
                detail.setSystemQuantity(1L);
                detail.setActualQuantity(1L);
                detail.setDifference(0L);
                detail.setBatchNo(inventoryRecords.getBatchNo());
                detail.setCheckId(inventoryRecords.getId());
                detail.setDeptId(departmentId);
                inventoryCheckDetails.add(detail);
            }
        }
        return inventoryCheckDetailMapper.insertInventoryCheckDetails(inventoryCheckDetails);
    }
    public int insertInventoryCheckMaterialDetails(InventoryRecords inventoryRecords) {
        List<InventoryCheckDetail> inventoryCheckDetails = new ArrayList<>();
        for (Long departmentId : inventoryRecords.getDepartmentIds()) {
            Asset asset = new Asset();
            asset.setDeptId(departmentId);
            MaterialListVo vo = new MaterialListVo();
            vo.setOffset(0);
            vo.setPageSize(10000);
            List<MaterialListVo> MaterialListVos = materialMapper.selectMaterial(vo);
            for (Material material : MaterialListVos.get(0).getMList()) {
                InventoryCheckDetail detail = new InventoryCheckDetail();
                detail.setAssetId(material.getInventoryId());
                detail.setLocationId(material.getWarehouseId());
                detail.setSystemQuantity(1L);
                detail.setActualQuantity(1L);
                detail.setDifference(0L);
                detail.setBatchNo(inventoryRecords.getBatchNo());
                detail.setCheckId(inventoryRecords.getId());
                inventoryCheckDetails.add(detail);
            }
        }

        return inventoryCheckDetailMapper.insertInventoryCheckDetails(inventoryCheckDetails);
    }
    /**
     * 修改资产盘点记录
     * 
     * @param inventoryRecords 资产盘点记录
     * @return 结果
     */
    @Override
    public int updateInventoryRecords(InventoryRecords inventoryRecords)
    {
        inventoryRecords.setUpdateTime(new Date());
        return inventoryRecordsMapper.updateInventoryRecords(inventoryRecords);
    }

    /**
     * 批量删除资产盘点记录
     * 
     * @param ids 需要删除的资产盘点记录主键
     * @return 结果
     */
    @Override
    public int deleteInventoryRecordsByIds(Long[] ids)
    {
        return inventoryRecordsMapper.deleteInventoryRecordsByIds(ids);
    }

    /**
     * 删除资产盘点记录信息
     * 
     * @param id 资产盘点记录主键
     * @return 结果
     */
    @Override
    public int deleteInventoryRecordsById(Long id)
    {
        return inventoryRecordsMapper.deleteInventoryRecordsById(id);
    }
}
